package com.example.wangfanwangluo.wangfanboss.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.wangfanwangluo.wangfanboss.Models.SearchModel;
import com.example.wangfanwangluo.wangfanboss.R;
import java.util.List;

/**
 * Created by wangfanwangluo on 2017/11/27.
 */

public class SearchAdapter extends BaseAdapter {

    private Context context; //获取上下文
    private List<SearchModel> list; //数据模型。。数组
    public SearchAdapter(Context context,List<SearchModel> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public SearchModel getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SearchModel model = getItem(position);
        View view;
        ViewHolder viewHolder; //相当于ios 中的contentview
        if (convertView == null) {
            //判断缓存池中是否有viewholder
            view = LayoutInflater.from(context).inflate(R.layout.searchitem, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.goodsID = (TextView)view.findViewById(R.id.goodsID_search);
            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.goodsID.setText(model.getGoodsID());
        return view;
    }
    class ViewHolder {
        TextView goodsID;
    }
}
